import {Request, Response, NextFunction, Router} from "express"; // 获取路由请求校验
import {Repository} from "sequelize-typescript"; // 获取数据库模型 ts校验
import {validationResult, body} from 'express-validator' // 获取express框架校验中间件
import {Swiper, Curriculum, Notice, Saying} from "../models"; // 获取数据库模型表
import {checkToken} from '../middlewares/jwt' // 获取jwt鉴权
import {getTable, getAnnounceList, getNews} from '../util/reptiles'  //获取其他路由接口工具

interface BodyRequestBody extends Request {
    body: {
        [key: string]: string | undefined
    }
}

// todo 这里有路由/校验待封装
const homeRouterFactory = (
    swiperRepository: Repository<Swiper>,
    curriculumRepository: Repository<Curriculum>,
    noticeRepository: Repository<Notice>,
    sayingRepository: Repository<Saying>,
    ) => Router()
        // 轮播
        .get('/getSwipe', async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                try {
                    const list = await swiperRepository.findAll()
                    if(list){
                        return res.json({
                            code: 1,
                            data: list
                        })
                    }

                    return res.json(list || {
                        errCode: 10001,
                        msg: '轮播获取失败'
                    })
                } catch (e) {
                    console.log(e);
                    next()
                }

            }
        )
        .post('/upSwipe', checkToken, [
                body('title').isLength({min: 1}),
                body('address').isLength({min: 1}),
                body('level').isLength({min: 1})
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                try {
                    const list = await swiperRepository.create({
                        title: req.body.title,
                        address: req.body.address,
                        level: req.body.level,
                    })
                    if (list) {
                        console.log('--- 上传成功 ---');
                        return res.json({
                            code: 1,
                            msg: '更新成功'
                        })
                    }
                    return res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                } catch (e) {
                    console.log(e);
                    next()
                }
            }
        )
        .post('/delSwipe', checkToken, [
                body('id').isLength({min: 1}),
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                try {
                    const id = req.body.id
                    if(!id) {
                        return res.json({
                            errCode: 10001,
                            msg: '操作失败'
                        });
                    }
                    const list = await swiperRepository.destroy({
                        where:{
                            id: id
                        }
                    })
                    if (list) {
                        console.log('--- 删除成功 ---');
                        return res.json({
                            code: 1,
                            msg: '删除成功'
                        })
                    }
                    return res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                } catch (e) {
                    console.log(e);
                    next()
                }
            }
        )
        // 课表
        .post('/getClass', checkToken, [
                body('account').isLength({min: 1}),
                body('password').isLength({min: 1})
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                try {
                    let {account, password} = req.body
                    if (account && password) {
                        let data = await getTable(account, password)
                        // console.log(data);
                        if(data){
                            return res.json({
                                code: 1,
                                data: data
                            })
                        }
                        res.json({
                            errCode: 10001,
                            msg: '操作失败'
                        })
                    }
                } catch (e) {
                    console.log(e);
                    res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                    next()
                }

            }
        )
        // .post('/upClass', [
        //         body('title_list').isLength({min: 1}),
        //         body('content_list').isLength({min: 1})
        //     ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
        //         const error = validationResult(req)
        //         if (!error.isEmpty()) {
        //             return res.status(422).json({
        //                 errCode: 10000,
        //                 msg: error.array()
        //             })
        //         }
        //         try {
        //             const list = await curriculumRepository.create({
        //                 title_list: req.body.title_list,
        //                 content_list: req.body.content_list,
        //             })
        //             if (list) {
        //                 console.log('--- 上传成功 ---');
        //                 return res.json({
        //                     code: 1,
        //                     msg: '更新成功'
        //                 })
        //             }
        //             return res.json({
        //                 errCode: 10001,
        //                 msg: '操作失败'
        //             })
        //         } catch (e) {
        //             console.log(e);
        //             res.json({
        //                 errCode: 10001,
        //                 msg: '操作失败'
        //             })
        //             next()
        //         }
        //     }
        // )

        // 通知公告
        .get('/getNotice', async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                try {
                    const list = await noticeRepository.findAll({
                        order: [
                            ['createdAt', 'DESC']
                        ]
                    })
                    if(list){
                        return res.json({
                            code: 1,
                            data: list
                        })
                    }
                    return res.json(list || {
                        errCode: 10001,
                        msg: '滚动公告获取失败'
                    })
                } catch (e) {
                    console.log(e);
                    next()
                }

            }
        )
        .post('/upNotice', checkToken, [
                body('content').isLength({min: 1})
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                let icon = req.body.icon
                try {
                    let list;
                    if (icon) {
                        list = await noticeRepository.create({
                            icon: icon,
                            content: req.body.content,
                        })
                    } else {
                        list = await noticeRepository.create({
                            content: req.body.content,
                        })
                    }

                    if (list) {
                        console.log('--- 上传成功 ---');
                        return res.json({
                            code: 1,
                            msg: '更新成功'
                        })
                    }
                    return res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                } catch (e) {
                    console.log(e);
                    next()
                }
            }
        )
        .post('/delNotice', checkToken, [
                body('id').isLength({min: 1}),
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                try {
                    const id = req.body.id
                    if(!id) {
                        return res.json({
                            errCode: 10001,
                            msg: '操作失败'
                        });
                    }
                    const list = await noticeRepository.destroy({
                        where:{
                            id: id
                        }
                    })
                    if (list) {
                        console.log('--- 删除成功 ---');
                        return res.json({
                            code: 1,
                            msg: '删除成功'
                        })
                    }
                    return res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                } catch (e) {
                    console.log(e);
                    next()
                }
            }
        )
        // 名人名言
        .get('/getSaying', async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                try {
                    const list = await sayingRepository.findAll()
                    if(list){
                        return res.json({
                            code: 1,
                            data: list
                        })
                    }
                    return res.json({
                        errCode: 10001,
                        msg: '数据获取失败'
                    })
                } catch (e) {
                    console.log(e);
                    next()
                }

            }
        )
        .post('/upSaying', checkToken, [
                body('content').isLength({min: 1}),
                body('auth').isLength({min: 1})
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                try {
                    let {content, auth} = req.body
                    const list = await sayingRepository.create({
                        content,
                        auth
                    })
                    if (list) {
                        console.log('--- 上传成功 ---');
                        return res.json({
                            code: 1,
                            msg: '上传成功'
                        })
                    }
                    return res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                } catch (e) {
                    console.log(e);
                    next()
                }
            }
        )
        .post('/delSaying', checkToken, [
                body('id').isLength({min: 1}),
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                try {
                    const id = req.body.id
                    if(!id) {
                        return res.json({
                            errCode: 10001,
                            msg: '操作失败'
                        });
                    }
                    const list = await sayingRepository.destroy({
                        where:{
                            id: id
                        }
                    })
                    if (list) {
                        console.log('--- 删除成功 ---');
                        return res.json({
                            code: 1,
                            msg: '删除成功'
                        })
                    }
                    return res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                } catch (e) {
                    console.log(e);
                    next()
                }
            }
        )
    // .get('/', async (req: BodyRequestBody, res: Response, next: NextFunction) => {
    //         const list = await homeRepository.findAll()
    //         res.json(list || {
    //             errCode: 10001,
    //             msg: '数据获取失败'
    //         })
    //     }
    //     // homeRepository.findAll()
    //     // .then(posts => res.json(posts))
    //     // .catch(next)
    // )

    // .get('/', async (req, res, next) => {
    //         const list = await homeRepository.findAll()
    //         res.json(list || {
    //             errCode: 10001,
    //             msg: '数据获取失败'
    //         })
    //     }
    //     // homeRepository.findByPk(req.params.id)
    //     // .then(post => post
    //     //   ? res.json(post)
    //     //   : next({statusCode: 404}))
    //     // .catch(next)
    // )
    //
    // .post('/posts', (req, res, next) =>
    //     homeRepository.create(req.body)
    //         .then(post => res.json(post))
    //         .catch(next)
    // )

;
export default homeRouterFactory
